mui.init({});
mui.plusReady(function() {
	var userId = plus.storage.getItem(KeyCfg.userId);
	(function initData() {
		var datas = {
			userId: userId
		};
		var url = UrlConfig.autobid_load;
		$.ajax(url, {
			data: datas,
			dataType: 'jsonp',
			crossDomain: true,
			success: function(data) {
				data = JSON.parse(data);
				loadData(data);
			}
		});
	})();
	//	readLoad();
	var sub = CmwMcore.$('sub');
	MeventMgr.onTouchstart(sub, function(event, self) {
		var subval = $("#sub").text();
		if (subval == '暂停自动投标') {
			var btn = ["确定", "取消"];
			mui.confirm('确定要暂停自动投标？', '温馨提示', btn, function(e) {
				if (e.index == 1) return;
				updateAutoBid();
			});
		} else {
			if (!valid()) return;
			submitForm();
		}
	});

	/*表单验证*/
	function valid() {
		var self = plus.webview.currentWebview();
		var cashBalance = $("#cashBalance").val();
		var acount = $("#acount").val();
		var tendamount = $("#tenderprice").val();
		var mintendamount = $("#minTenderPrice").val();
		var yearrate = $("#yearrate").val();
		var bbmend = $("#bbmend").val();
		var reg = /^(([1-9]\d*)|\d)?$/;
		if (mintendamount == "" || mintendamount < 100 || tendamount == "" || tendamount < 100 || tendamount > 20000) {
			mui.alert('请输入单笔投标金额，金额在100元至20000元之间的整数，输入格式参照:100元至20000元');
			return false;
		} else if (mintendamount - tendamount >= 0) {
			mui.alert('请输入单笔投标金额，金额在100元至20000元之间的整数，输入格式参照:100元至20000元');
			return false;
		}
		if (!reg.test(mintendamount)) {
			mui.alert('请输入单笔投标金额，金额在100元至20000元之间的整数，输入格式参照:100元至20000元');
			return false;
		}
		if ('' != acount) {
			if (!reg.test(acount)) {
				mui.alert('请输入正确的数字！');
				return false;
			} else if (acount - cashBalance > 0) {
				mui.alert('请输入账户保留金额,且不可大于账户可用余额的整数');
				return false;
			}
		} else {
			mui.alert('账户保留金额不能为空');
			return false;
		}

		if ('' != yearrate) {
			if (!reg.test(yearrate)) {
				mui.alert('请输入年化利率下限,请输入0-13内的整数')
				return false;
			} else if (yearrate > 13) {
				mui.alert('请输入年化利率下限,请输入0-13内的整数');
				return false;
			}
		}else{
			mui.alert('请输入年化利率下限,请输入0-13内的整数');
			return false;
		}

		if ('' != $('#bbmend').val()) {
			if (!reg.test(bbmend)) {
				mui.alert('请输入借款期限上限,请输入0-36内的整数');
				return false;
			} else if (bbmend > 36) {
				mui.alert('请输入借款期限上限,请输入0-36内的整数');
				return false;
			}
		}
		return true;
	}

	function submitForm() {
		//		CmwMcore.disabled('sub');	
		var formDatas = $("#theForm").serialize();
		var url = UrlConfig.autobid_save;
		MuiEventMgr.ajax(url, {
			pars: formDatas,
			sfn: function(data) {
				callback(1);
				displayStatus(1);
				mui.alert('已成功开启自动投标！');
			}
		});

	}

	function callback(state) {
		var ips = document.getElementsByTagName("input");
		for (var i = 0; i < ips.length; i++) {
			if (ips[i].getAttribute('type').toLowerCase() == 'text') {
				if (state == 1) {
					ips[i].readOnly = true;
				} else {
					ips[i].readOnly = false;
				}
			}
		}
	}

	function updateAutoBid() {
		var pars = {};
		var url = UrlConfig.autobid_update;
		MuiEventMgr.ajax(url, {
			pars: pars,
			sfn: function(data) {
				callback(2);
				displayStatus();
				mui.alert('自动投标已暂停！');
			}
		});
	}

	//	function readLoad(){
	//		var userId = plus.storage.getItem(KeyCfg.userId);
	//		var datas={userId:userId};
	//		var url = UrlConfig.autobid_load;
	//		$.ajax(url,{
	//			data:datas,
	//			dataType : 'jsonp', 
	//			crossDomain: true,
	//			success:function(data){
	//				data = JSON.parse(data);
	//				loadData(data);
	//			}
	//		});
	//	}

	function loadData(data) {
		var success = data['success'];
		if (success == false) mui.alert('获取账户数据失败！');return;
		var acount = data['acount'];
		var tenderprice = data['tenderprice'];
		var mintenderprice = data['mintenderprice'];
		var bbmend = data['bbmend'];
		var yearrate = data['yearrate'];
		var state = data["state"];
		var cashBalance = data["cashBalance"];
		$('#acount').val(acount);
		$('#tenderprice').val(tenderprice);
		$('#minTenderPrice').val(mintenderprice);
		$('#bbmend').val(bbmend);
		$('#yearrate').val(yearrate);
		$('#cashBalance').val(cashBalance);
		displayStatus(state);
		callback(state);
	}

	function displayStatus(state) {
		var subHtml = "开启自动投标";
		var stateHtml = "已关闭";
		var ele_status = $("#status");
		var bgColor = "#E70010";
		if (state && state == 1) {
			subHtml = '暂停自动投标';
			stateHtml = '已开启';
			bgColor = "#E70010";
		}
		ele_status.css("background-color", bgColor);
		$("#sub").html(subHtml);
		ele_status.html(stateHtml);
	}
});